import HttpServiceInstance from '@/utils/axios'
import { AxiosServiceConfig, ResultVo } from '@/utils/axios/types/axios-config'
import { AxiosError, AxiosResponse, Method } from 'axios'

/**
 * 发送POST请求，默认不带消息
 * @param url 
 * @param data 
 * @param options 
 * @returns 
 */
export function requestPost<T = any>(url: string, data?: any, options?: AxiosServiceConfig): Promise<ResultVo<T>> {
    return request<T>(url, "POST", data, options)
}
/**
 * 发送POST请求，带消息
 * @param url 
 * @param data 
 * @param options 
 * @returns 
 */
export function requestPostMsg<T = any>(url: string, data?: any, options?: AxiosServiceConfig): Promise<ResultVo<T>> {
    const optiones: AxiosServiceConfig = {
        isSuccessMsg: true,
        isErrorMsg: true,
        ...options
    }
    return request<T>(url, "POST", data, optiones)
}


export function requestGet<T = any>(url: string, options?: AxiosServiceConfig): Promise<ResultVo<T>> {
    return request<T>(url, "GET", {}, options)
}

export function requestGetMsg<T = any>(url: string, options?: AxiosServiceConfig): Promise<ResultVo<T>> {
    const optiones: AxiosServiceConfig = {
        isSuccessMsg: true,
        isErrorMsg: true,
        ...options
    }
    return request<T>(url, "GET", {}, optiones)
}


/**
 * 请求封装默认值
 * @param url 
 * @param method 
 * @param data 
 * @param options 
 * @returns 
 */
export function request<T = any>(url: string, method: Method, data?: any, options?: AxiosServiceConfig): Promise<ResultVo<T>> {
    const config: AxiosServiceConfig = {
        url: url,
        method: method,
        data: data,
        isToken: true,
        successMsg: "操作成功",
        errorMsg: "操作失败",
        loadingText: "加载中。。。",
        ...options
    }
    return HttpServiceInstance(config)
}



